<template>
	<view class="content b-t">
		<view class="list b-b" v-for="(item, index) in addressList" :key="index" @click="checkAddress(item)">
			<view class="wrapper">
				<view class="address-box">
					<text class="address">{{item.receiver_name}} {{item.receiver_mobile}}</text>
				</view>
				<view class="u-box">
					<text class="name">{{item.receiver_province}}{{item.receiver_city}}{{item.receiver_district}}</text>
					<text class="mobile">{{item.receiver_address}}</text>
				</view>
			</view>
			<up-divider></up-divider>
			<view style="display: flex;justify-content: flex-end;">

				<view class="yticon icon-bianji" @click.stop="addAddress('edit', item)"> <up-icon
						name="edit-pen"></up-icon>编辑</view>
				<view class="yticon icon-bianji" @click.stop="addAddress('del', item)"> <up-icon name="trash"></up-icon>
					删除</view>
			</view>

		</view>


		<button class="add-btn" @click="addAddress('add')">添加新地址</button>
	</view>
</template>

<script>
	import {
		http
	} from 'uview-plus'
	export default {
		data() {
			return {
				source: 0,
				addressList: []
			}
		},
		async onLoad(option) {
			console.log(option.source);
			this.source = option.source;

		},
		async onShow() {
			this.addList()
		},
		methods: {
			async addList(){
				this.addressList = await http.get('/shop/user/address', {
					header: {
						'Content-Type': 'multipart/form-data'
					}
				});
			},
			//选择地址
			checkAddress(item) {
				if (this.source == 1) {
					//this.$api.prePage()获取上一页实例，在App.vue定义
					this.$api.prePage().addressData = item;
					uni.navigateBack()
				}
			},
			addAddress(type, item) {
				var that = this
				if (type == 'del') {
					uni.showModal({
						content: '确定删除该地址？？',
						success: function(res) {
							if (res.confirm) {
								that.delAddress(item)
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					})
					return
				}
				uni.navigateTo({
					url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(item)}`
				})
			},
			async delAddress(item){
				var arr = Array();
				arr.push(item.id)
				await http.post('/shop/user/cart/delete',{user_cart_ids:arr}).then(data => {
					uni.showToast({
						title:'删除成功',
						icon:'none'
					})
					this.addList()
				});
			},
			//添加或修改成功之后回调
			refreshList(data, type) {
				//添加或修改后事件，这里直接在最前面添加了一条数据，实际应用中直接刷新地址列表即可
				this.addressList.unshift(data);

				console.log(data, type);
			}
		}
	}
</script>

<style lang='scss'>
	page {
		padding-bottom: 120rpx;
		background: #F1F2F6;
	}

	.content {
		position: relative;
	}

	.list {
		width: 718rpx;
		min-height: 216rpx;
		border-radius: 20rpx;
		padding: 20rpx 30rpx;
		;
		background: #fff;
		position: relative;
		margin: 20rpx auto;
		box-sizing: border-box;
	}

	.wrapper {
		display: flex;
		flex-direction: column;
		flex: 1;
	}

	.address-box {
		display: flex;
		align-items: center;

		.tag {
			font-size: 24rpx;
			color: $base-color;
			margin-right: 10rpx;
			background: #fffafb;
			border: 1px solid #ffb4c7;
			border-radius: 4rpx;
			padding: 4rpx 10rpx;
			line-height: 1;
		}

		.address {
			font-size: 30rpx;
			color: $font-color-dark;
		}
	}

	.u-box {
		font-size: 28rpx;
		color: $font-color-light;
		margin-top: 16rpx;

		.name {
			margin-right: 30rpx;
		}
	}

	.icon-bianji {
		display: flex;
		align-items: center;
		font-size: 24rpx;
		font-weight: normal;
		color: #3D3D3D;
		padding-left: 30rpx;
	}

	.add-btn {
		position: fixed;
		left: 30rpx;
		right: 30rpx;
		bottom: 16rpx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690rpx;
		height: 88rpx;
		border-radius: 200rpx;
		opacity: 1;

		background: #08AFFE;

		border: 0px solid #08AFFE;
		font-size: 32rpx;
		color: #fff;
	}
</style>